import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class Test {
    public int[] finalPrices(int[] prices) {
        int len = prices.length;
        int[] ans = new int[len];
        Stack<Integer> stack = new Stack<>();
        for(int i = len - 1; i >= 0; i--) {
            while(!stack.empty() && stack.peek() > prices[i]) {
                stack.pop();
            }
            ans[i] = stack.empty() ? prices[i] : prices[i] - stack.peek();
            stack.push(prices[i]);
        }
        return ans;
    }

}
